load("@bazel_gazelle//:def.bzl", "gazelle_binary")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load(":gazelle_test.bzl", "gazelle_test")

go_library(
    name = "python",
    srcs = [
        "configure.go",
        "file_parser.go",
        "fix.go",
        "generate.go",
        "kinds.go",
        "language.go",
        "parser.go",
        "resolve.go",
        "std_modules.go",
        "target.go",
    ],
    # NOTE @aignas 2023-12-03: currently gazelle does not support embedding
    # generated files, but 3.11.txt is generated by a build rule.
    #
    # You will get a benign error like when running gazelle locally:
    # > 8 gazelle: .../rules_python/gazelle/python/std_modules.go:24:3: pattern 3.11.txt: matched no files
    #
    # See following for more info:
    # https://github.com/bazelbuild/bazel-gazelle/issues/1513
    embedsrcs = ["stdlib_list.txt"],  # keep # TODO: use user-defined version?
    importpath = "github.com/bazelbuild/rules_python/gazelle/python",
    visibility = ["//visibility:public"],
    deps = [
        "//manifest",
        "//pythonconfig",
        "@bazel_gazelle//config:go_default_library",
        "@bazel_gazelle//label:go_default_library",
        "@bazel_gazelle//language:go_default_library",
        "@bazel_gazelle//repo:go_default_library",
        "@bazel_gazelle//resolve:go_default_library",
        "@bazel_gazelle//rule:go_default_library",
        "@com_github_bazelbuild_buildtools//build:go_default_library",
        "@com_github_bmatcuk_doublestar_v4//:doublestar",
        "@com_github_emirpasic_gods//lists/singlylinkedlist",
        "@com_github_emirpasic_gods//sets/treeset",
        "@com_github_emirpasic_gods//utils",
        "@com_github_smacker_go_tree_sitter//:go-tree-sitter",
        "@com_github_smacker_go_tree_sitter//python",
        "@org_golang_x_sync//errgroup",
    ],
)

copy_file(
    name = "stdlib_list",
    src = select(
        {
            "@rules_python//python/config_settings:is_python_3.10": "@python_stdlib_list//:stdlib_list/lists/3.10.txt",
            "@rules_python//python/config_settings:is_python_3.11": "@python_stdlib_list//:stdlib_list/lists/3.11.txt",
            "@rules_python//python/config_settings:is_python_3.12": "@python_stdlib_list//:stdlib_list/lists/3.12.txt",
            "@rules_python//python/config_settings:is_python_3.8": "@python_stdlib_list//:stdlib_list/lists/3.8.txt",
            "@rules_python//python/config_settings:is_python_3.9": "@python_stdlib_list//:stdlib_list/lists/3.9.txt",
            # This is the same behaviour as previously
            "//conditions:default": "@python_stdlib_list//:stdlib_list/lists/3.11.txt",
        },
    ),
    out = "stdlib_list.txt",
    allow_symlink = True,
)

# gazelle:exclude testdata/

gazelle_test(
    name = "python_test",
    srcs = ["python_test.go"],
    data = [
        ":gazelle_binary",
    ],
    test_dirs = glob(
        # Use this so that we don't need to manually maintain the list.
        ["testdata/*"],
        exclude = ["testdata/*.md"],
        # The directories aren't inputs themselves; we just want their
        # names.
        exclude_directories = 0,
    ),
    deps = [
        "@bazel_gazelle//testtools:go_default_library",
        "@com_github_ghodss_yaml//:yaml",
        "@io_bazel_rules_go//go/runfiles:go_default_library",
        "@io_bazel_rules_go//go/tools/bazel:go_default_library",
    ],
)

gazelle_binary(
    name = "gazelle_binary",
    languages = [":python"],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "distribution",
    srcs = glob(["**"]),
    visibility = ["//:__pkg__"],
)

go_test(
    name = "default_test",
    srcs = [
        "file_parser_test.go",
        "std_modules_test.go",
    ],
    embed = [":python"],
    deps = [
        "@com_github_stretchr_testify//assert",
    ],
)
